package com.review.reviewsb.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.review.reviewsb.config.WXConfig;
import com.review.reviewsb.task.AsyncTask;
import com.review.reviewsb.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @program: reviewSb
 * @description: 测试
 * @author: YaoZhiLi
 * @create: 2021-07-03 23:21
 */

@RestController
@RequestMapping("api/v1/test")
@PropertySource({"classpath:pay.properties"})
public class TestController {
    @Value("${wxpay.appid}")
    private String payAppid;
    @Value("${wxpay.sercret}")
    private String paySecret;
    @Autowired
    private AsyncTask asyncTask;
    @Autowired
    private WXConfig wXConfig;
    @GetMapping("get_config")
    public JsonData testConfig() {
//        Map<String, String> map = new HashMap<>();
//        map.put("appid",payAppid);
//        map.put("secret",paySecret);
//        return JsonData.buildSuccess(map);
        return JsonData.buildSuccess(wXConfig.getPayAppid());
    }

    @GetMapping("list")
    public JsonData testExt(){
        int i=1/0;
        return JsonData.buildSuccess("");
    }

    @GetMapping("async")
    public JsonData testAsync(){
        long begin = System.currentTimeMillis();
//        asyncTask.task1();
//        asyncTask.task2();
//        asyncTask.task3();
//        asyncTask.task4();
        Future<String> task5 = asyncTask.task5();
        Future<String> task6 = asyncTask.task6();
        for (;;){
            if(task5.isDone()&&task6.isDone()){
                try {
                    String s = task5.get();
                    String s1 = task6.get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }finally {
                    break;
                }
            }
        }
        return JsonData.buildSuccess(begin);
    }
}